package chapter11;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-22 下午3:03
 */
public class OutPutStreamTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(OutPutStreamTest.class);

    public static void main(String[] args) {
        File file = new File(File.separator + "home" + File.separator + "lzy" + File.separator + "logs" + File.separator + "test" + File.separator + "test.dat");

        if (!file.getParentFile().exists()) {
            LOGGER.info("create directory {}:{}", file.getParent(), file.getParentFile().mkdirs());
        }

        try (OutputStream outputStream = new FileOutputStream(file, true);
             Writer out = new OutputStreamWriter(outputStream)) {

            out.append("this is appended by outputstreamwriter.\n\r");

            String str = "this is test content\n\r";
            byte[] data = str.getBytes();
            outputStream.write(data, 6, 6);

        } catch (IOException e) {
            e.printStackTrace();
        }

        int len = 0;
        byte[] readInDat = new byte[20];
        try (InputStream inputStream = new FileInputStream(file)) {
            while ((len = inputStream.read(readInDat)) != -1) {
                LOGGER.info("readout:{}", new String(readInDat, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
